package com.ktkj.init.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;


/**
 * The type Response.
 *
 * @author :yanlixiong
 * @date :2021-10-25 13:25:24
 */
@ApiModel
public class Response implements Serializable {

    private static final long serialVersionUID = -3799117183776657761L;

    public static final int SUCCESS = 200;

    @ApiModelProperty(value = "响应报文的响应码，code==200即为正常响应", example = "200")
    private int code;

    @ApiModelProperty(value = "响应报文的消息提示", example = "成功")
    private String message;

    @ApiModelProperty(value = "响应报文的消息体", example = "true")
    private Object body;

    @ApiModelProperty(value = "响应报文的时间戳", example = "1635142060196")
    private Long timestamp;

    public Response() {
    }

    public Response(Object body) {
        this(SUCCESS, "成功", body);
    }

    public Response(int code, String message, Object body) {
        this.code = code;
        this.message = message;
        this.body = body;
        timestamp = System.currentTimeMillis();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getBody() {
        return body;
    }

    public Response setBody(Object data) {
        this.body = data;
        return this;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    /*@JsonIgnore
    public String getJsonBody() {
        return JSONObject.toJSONString(body);
    }

    @JsonIgnore
    public <E> E getObjectBody(Class<E> clazz) {
        String json = getJsonBody();
        return JSONObject.parseObject(json, clazz);
    }

    @JsonIgnore
    public <E> List<E> getListBody(Class<E> clazz) {
        String json = getJsonBody();
        return JSONObject.parseArray(json, clazz);
    }*/

    public static Response ok(Object body) {
        return new Response(body);
    }

    public static Response error(int code, String message) {
        return new Response(code, message, null);
    }

    @Override
    public String toString() {
        return "Response [code=" + code + ", message=" + message + ", body=" + body + ", timestamp=" + timestamp + "]";
    }
}

